
import Mock from "mockjs"


// const phone = Mock.mock(/^1[3456789]\d{9}$/);

// const number = Mock.mock(/^\d{5}$/) 
const {readerList} = Mock.mock({
    
            'readerList|10':[
                {
                  'id':'@guid()',
                  'name': "@cname()",
                  'phone':'@integer(10000000000,19999999999)',
                  'number':'@integer(10000,19999)',
                  'arrears':'0.00元',
                  'deposit':'@integer(100.00,200.00)'+'元',
                  'sex|1':['男','女']
                }
            ]
})
Mock.mock('/api/getReadersList','get',()=>{

    return {
        data:readerList,
        msg:'数据获取成功',
        code:200
      
    }
})


Mock.mock('/api/deleteReader', 'post', (options:any) => {
    const req = JSON.parse(options.body);
    console.log(req);
    
  
    // let data = readerList.filter((item:any) => item.number !== req.number);

    // return {
    //     data:data,
    //     code: 200,
    //     message: '删除成功'
    // }
    const res = {
        code: 200,
        message: '删除失败',
        data:readerList
    }
    for (let i = 0; i < readerList.length; i++) {
        
       if(readerList[i].number == req.number){
        // 在数据中删除掉匹配的内容
        readerList.splice(i, 1)

        res.message = '删除成功'
        break
       } 
    }
    return res
  })

  // 新增读者
Mock.mock('/api/addReader', 'post', (config:any)=>{

    // console.log(JSON.parse(config.body))

    // 获取请求的参数
    const req = JSON.parse(config.body);
    readerList.push(req)

    return {
        data:readerList,
        msg: '数据添加成功',
        code: 200
    }
})

